/*
    File:       SocketUtils.h

    Contains:   Some static routines for dealing with networking
                    

*/

#ifndef __SOCKETUTILS_H__
#define __SOCKETUTILS_H__

#ifndef __Win32__
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#endif

#include "OSHeaders.h"
#include "MyAssert.h"
#include "StrPtrLen.h"

#include <string>

class SocketUtils
{
    public:

        // Call initialize before using any socket functions.
        // (pass true for lookupDNSName if you want the hostname
        // looked up via DNS during initialization -- %%sfu)
        static void Initialize(Bool lookupDNSName = true);
		static void Uninitialize();
        
        //static utility routines
        static Bool   IsMulticastIPAddr(UInt32 inAddress);
        static Bool   IsLocalIPAddr(UInt32 inAddress);

		static Bool	GetPeerInfo(Int32 vFD, std::string &vIP, UInt16 &vPort);

        //This function converts an integer IP address to a dotted-decimal string.
        static void ConvertAddrToString(const struct in_addr& theAddr, std::string* outAddr);
        
        // This function converts a dotted-decimal string IP address to a UInt32
        static UInt32 ConvertStringToAddr(const char* inAddr);
        
        //You can get at all the IP addrs and DNS names on this machine this way
        static UInt32				GetNumIPAddrs() { return sNumIPAddrs; }
        static inline UInt32        GetIPAddr(UInt32 inAddrIndex);

		//DNS: return----host-ordered int IP
		static UInt32	HostNameToIP(const std::string& strHostName);
		static std::string	HostNameToStringIP(const std::string& strHostName);
		
		//convet Int IP to string IP
		static std::string IPAddrToString(unsigned long nIpAddr);

			

	private:
		friend class Socket;
        static inline StrPtrLen*    GetIPAddrStr(UInt32 inAddrIndex);
        static inline StrPtrLen*    GetDNSNameStr(UInt32 inDNSIndex);
            
    private:

        //Utility function used by Initialize
#ifndef __Win32__
        static Bool IncrementIfReqIter(char** inIfReqIter, ifreq* ifr);
#endif
        //For storing relevent information about each IP interface
        struct IPAddrInfo
        {
            UInt32      fIPAddr;
            StrPtrLen   fIPAddrStr;
            StrPtrLen   fDNSNameStr;
        };
        
        static IPAddrInfo*              sIPAddrInfoArray;
        static UInt32                   sNumIPAddrs;
};

inline UInt32   SocketUtils::GetIPAddr(UInt32 inAddrIndex)
{
    Assert(sIPAddrInfoArray != NULL);
    Assert(inAddrIndex < sNumIPAddrs);
    return sIPAddrInfoArray[inAddrIndex].fIPAddr;
}

inline StrPtrLen*   SocketUtils::GetIPAddrStr(UInt32 inAddrIndex)
{
    Assert(sIPAddrInfoArray != NULL);
    Assert(inAddrIndex < sNumIPAddrs);
    return &sIPAddrInfoArray[inAddrIndex].fIPAddrStr;
}

inline StrPtrLen*   SocketUtils::GetDNSNameStr(UInt32 inDNSIndex)
{
    Assert(sIPAddrInfoArray != NULL);
    Assert(inDNSIndex < sNumIPAddrs);
    return &sIPAddrInfoArray[inDNSIndex].fDNSNameStr;
}

#endif // __SOCKETUTILS_H__

